﻿@inject IDialogService DialogService
@inject IDataConfigService DataConfigService

<MudTable @ref="_requestTable" ServerData="@(new Func<TableState, Task<TableData<ConfigFieldInfoResponseDto>>>(ServerReload))"
          MultiSelection="true" Hover="true" Dense="true" Breakpoint="Breakpoint.Xs" Elevation="0"
          ReadOnly="true" FixedHeader="true" CustomFooter="true" HorizontalScrollbar="true">
    <ToolBarContent>
        <MudButton Color="Color.Primary" Variant="Variant.Filled" Size="Size.Small" OnClick="@(_ => AddOrUpdateHandler())">新增字段</MudButton>
        @* <MudSpacer></MudSpacer> *@
        @* <MudIconButton Icon="@Icons.Material.Outlined.Refresh" OnClick="QueryHandler"></MudIconButton> *@
    </ToolBarContent>
    <Columns>
        <SelectColumn T="ConfigFieldInfoResponseDto"/>
        <MudColumn Value="@context.FieldCode" HeaderText="字段编码"/>
        <MudColumn Value="@context.FieldName" HeaderText="字段名称"/>
        <MudColumn Value="@context.DataType" HeaderText="数据类型"/>
        <MudColumn Value="@context.ValueColumnMap" HeaderText="存储字段"/>
        <MudColumn Value="@context.ControlType" HeaderText="控件类型"/>
        <MudColumn Value="@context.IsRequired" HeaderText="必填"/>
        <MudColumn Value="@context.IsKey" HeaderText="主键"/>
        <MudColumn Value="@context.IsVisible" HeaderText="可见"/>
        <MudColumn Value="@context.Sort" HeaderText="排序"/>
        <MudTemplateColumn T="DataConfigInfoResponseDto">
            <Header Context="_">
                <MudTh>操作</MudTh>
            </Header>
            <Row Context="_">
                <MudTd>
                    <MudButton StartIcon="@Icons.Material.Filled.Edit" Size="Size.Small" OnClick="@(() => AddOrUpdateHandler(context.Key))">修改</MudButton>
                    <MudButton StartIcon="@Icons.Material.Filled.Remove" Size="Size.Small">删除</MudButton>
                </MudTd>
            </Row>
        </MudTemplateColumn>
    </Columns>
    <PagerContent>
        <MudTablePager/>
    </PagerContent>
</MudTable>

@code {

    private MudTable<ConfigFieldInfoResponseDto> _requestTable = new();
    private ConfigFieldSearchRequestDto _searchRequestDto = new();

    [CascadingParameter]
    public DataConfigIndex? DataConfigIndex { get; set; }

    private async Task<TableData<ConfigFieldInfoResponseDto>> ServerReload(TableState state)
    {
        var dataConfigCode = DataConfigIndex?.GetDataConfigCode();

        _searchRequestDto.Page = state.Page + 1;
        _searchRequestDto.Limit = state.PageSize;
        var result = await DataConfigService.QueryDataConfigFieldsByPage(dataConfigCode!, _searchRequestDto);

        var totalCount = 0;
        IList<ConfigFieldInfoResponseDto> tableData = new List<ConfigFieldInfoResponseDto>();

        if (result.Succeeded)
        {
            tableData = result.Data;
            totalCount = result.TotalCount;
        }

        return new TableData<ConfigFieldInfoResponseDto>() { TotalItems = totalCount, Items = tableData };
    }

    Task QueryHandler()
    {
        return Task.CompletedTask;
    }

    async Task AddOrUpdateHandler(Guid? id = null)
    {
        var dataConfigCode = DataConfigIndex?.GetDataConfigCode();

        var editData = new ConfigFieldEditRequestDto();
        var title = id == null ? "新增字段" : "修改字段";

        var dialog = await DialogService.ShowAsync<DataConfigFieldEdit>(title,
            new DialogParameters
            {
                ["Key"] = id,
                ["ConfigCode"] = dataConfigCode
            },
            new DialogOptions
            {
                DisableBackdropClick = true,
                MaxWidth = MaxWidth.Medium,
                FullWidth = true
            });
        var result = await dialog.Result;
        if (!result.Canceled)
        {
            await _requestTable.ReloadServerData();
        }
    }

    async Task DeleteHandler(Guid id)
    {
        await DialogService.ShowMessageBox(
            "提示",
            "确定删除选中的用户?",
            yesText: "确定", cancelText: "取消");
    }

}